package com.scnu.yxp.travelapp;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.scnu.yxp.travelapp.homepage.HomePageActivity;
import com.scnu.yxp.travelapp.tool.httpUtil;

public class LoggingActivity extends Activity {
	//登陆按钮
	private ImageButton loggingBtn;
	//邮箱账号和密码
	private EditText mailAccount, password;
	//飞机图像
	private ImageView plane;
	//帧动画
	private AnimationDrawable anim;
	private String response;
	private InputMethodManager mInputMethodManager;
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 0x120)
			{
				if(response.contains("ok"))
				{
					Intent intent = new Intent(LoggingActivity.this, HomePageActivity.class);
					startActivity(intent);
				}else{
					Toast.makeText(LoggingActivity.this, "账号或密码不正确", Toast.LENGTH_SHORT).show();
					mailAccount.setText("");
					password.setText("");
					anim.stop();
				}
			}
		}};
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_logging);
        loggingBtn = (ImageButton)findViewById(R.id.logging_btn);
        mailAccount = (EditText)findViewById(R.id.mail);
        password = (EditText)findViewById(R.id.password);
        plane = (ImageView)findViewById(R.id.plane);
        //帧动画设置
        plane.setBackgroundResource(R.drawable.plane_move_anim);
        anim = (AnimationDrawable)plane.getBackground();
        mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        loggingBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				anim.start();
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						response = httpUtil.sendPost("http://2.travelaround.sinaapp.com/handledenglu",
								"user.name=" + mailAccount.getText().toString() + "&user.password=" +
										password.getText().toString());
						handler.sendEmptyMessage(0x120);
					}
				});
				thread.start();
				mInputMethodManager.hideSoftInputFromWindow(getCurrentFocus()
						.getApplicationWindowToken(), 0);
			}
		});
    }


    

}
